﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using LibrarySolution.DAO.委托;
using LibrarySolution.ComMethod;
using LibrarySolution.DAO.基础;
using LibrarySolution.Model.基础;

namespace LibrarySolution.Frm
{
    public partial class FrmBkxxModify : Form
    {
        #region 属性
        Delegates delegates = new Delegates();
        BkxxDao bkxxDao = new BkxxDao();
        public static bool IsModify = true;
        private static FrmBkxxModify Instance;
        #endregion

        #region 构造器
        public FrmBkxxModify()
        {
            InitializeComponent();
        }
        #endregion

        #region 事件
        public static FrmBkxxModify GetInstance(Delegates delegates)
        {
            Instance = new FrmBkxxModify();
            if (IsModify)
            {
                Instance.BindingData();
                if (Instance.LblGetBkState.Text == "丢失")
                {
                    Instance.panel1.Enabled = false;
                }
                if (Instance.LblGetBkState.Text == "外借")
                {
                    Instance.panel1.Enabled = false;
                }
            }
            else
            {
                Instance.LblGetBkState.Text = "在库";
                Instance.LblGetRksj.Text = DataUtil.getTime();
                Instance.LblGetBkCode.Text = Instance.bkxxDao.getMaxCode();
                Instance.TxtGetBkNo.Text = Instance.bkxxDao.getMaxNo();
            }
            if (GlobalVar.YhEnter == true)
            {
                Instance.toolStrip1.Visible = false;
                Instance.panel1.Enabled = false;
            }
            Instance.delegates = delegates;
            Instance.BringToFront();
            return Instance;
        }

        private void TstripBtnDone_Click(object sender, EventArgs e)
        {
            BkxxDetail bkxxDetail = new BkxxDetail();
            bkxxDetail.bk_code = LblGetBkCode.Text;
            bkxxDetail.bk_no = TxtGetBkNo.Text;
            bkxxDetail.bk_name=TxtBkName.Text;
            bkxxDetail.bk_writer = TxtWriter.Text;
            if (TxtPrice.Text == "") TxtPrice.Text = "0";
            bkxxDetail.bk_price = TxtPrice.Text;
            if (DataUtil.formattime(TxtCbsj.Text) != "Wrong")
                bkxxDetail.bk_cbsj = DataUtil.formattime(TxtCbsj.Text);
            else
            {
                MessageBox.Show("请输入正确的日期格式(xxxx-xx-xx)");
                return;
            }
            bkxxDetail.bk_publish = TxtPublish.Text;
            bkxxDetail.bk_yhname = LblGetBkYhName.Text;
            bkxxDetail.bk_rksj = LblGetRksj.Text;
            bkxxDetail.bk_state = LblGetBkState.Text;
            if (IsModify)
            {
                bkxxDao.UpdateBkxx(bkxxDetail);
                MessageBox.Show("修改成功");
            }
            else
            {
                bkxxDao.insertBkxx(bkxxDetail);
                MessageBox.Show("添加成功");
            }
            if (Instance.delegates != null)
                Instance.delegates.doWork();
            this.Close();
        }

        private void TxtPrice_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!(Char.IsNumber(e.KeyChar) || e.KeyChar == '\b' || e.KeyChar == '.'))
            {
                e.Handled=true;
            }
        }

        private void TstripBtnCancle_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        #endregion

        #region 方法
        void BindingData()
        {
            LblGetBkCode.Text = GlobalVar.BkxxDetail.bk_code;
            TxtGetBkNo.Text = GlobalVar.BkxxDetail.bk_no;
            TxtBkName.Text = GlobalVar.BkxxDetail.bk_name;
            TxtWriter.Text = GlobalVar.BkxxDetail.bk_writer;
            TxtPrice.Text = GlobalVar.BkxxDetail.bk_price;
            TxtCbsj.Text = GlobalVar.BkxxDetail.bk_cbsj;
            TxtPublish.Text = GlobalVar.BkxxDetail.bk_publish;
            LblGetBkState.Text = GlobalVar.BkxxDetail.bk_state;
            LblGetBkYhName.Text = GlobalVar.BkxxDetail.bk_yhname;
            LblGetRksj.Text = DataUtil.getTime();
        }
        #endregion

    }
}
